﻿#include "buss_comment.h"
#include "ui_buss_comment.h"
#include "buss_main.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QVBoxLayout>
#include <QLabel>
#include <QScreen>
#include <QGuiApplication>

Buss_comment::Buss_comment(int bus_id, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Buss_comment),
    bus_id(bus_id)
{
    ui->setupUi(this);
    setWindowTitle("评论管理");
    
   // 设置窗口大小
    resize(1200, 800);
    setMinimumSize(1000, 600);
    
    loadComments();
}

Buss_comment::~Buss_comment()
{
    delete ui;
}

//跳转菜单主界面
void Buss_comment::on_pushButton_return_clicked()
{
    Buss_main *b=new Buss_main;
    if (bus_id > 0) {
        b->setBusiness1_id(bus_id);
    }
    b->show();
    this->hide();
    return;
}

void Buss_comment::loadComments()
{
    // 查询本商家所有商品的评论，且订单已完成
    QString sql = "SELECT c.com_name, u.user_name, cm.comment, cm.time "
                  "FROM comment cm "
                  "JOIN commodity c ON cm.com_id = c.com_id "
                  "JOIN user u ON cm.user_id = u.user_id "
                  "JOIN order_c o ON cm.com_id = o.com_id AND cm.user_id = o.user_id "
                  "WHERE c.business_id = :business_id AND o.cs_id = 8 "
                  "ORDER BY cm.time DESC";
    QSqlQuery query;
    query.prepare(sql);
    query.bindValue(":business_id", bus_id);
    if (!query.exec()) {
        QLabel *err = new QLabel("评论加载失败: " + query.lastError().text());
        QVBoxLayout *errLayout = new QVBoxLayout;
        errLayout->addWidget(err);
        QWidget *errWidget = new QWidget;
        errWidget->setLayout(errLayout);
        ui->scrollArea->setWidget(errWidget);
        return;
    }
    QWidget *content = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(content);
    while (query.next()) {
        QString info = QString("商品: %1 | 用户: %2\n内容: %3\n时间: %4")
            .arg(query.value(0).toString())
            .arg(query.value(1).toString())
            .arg(query.value(2).toString())
            .arg(query.value(3).toString());
        QLabel *label = new QLabel(info);
        label->setWordWrap(true);
        layout->addWidget(label);
    }
    content->setLayout(layout);
    ui->scrollArea->setWidget(content);
}
